package com.jasiri.project.analysis

import com.jasiri.project.app.App
import com.jasiri.project.base.BaseApp
import com.jasiri.project.lib.analysis.AnalysisBuilder
import com.jasiri.project.lib.analysis.AnalyticsUtil
import com.jasiri.project.lib.dev.JasiriSysDataHelper
import com.jasiri.project.utils.config.Configs
import com.jasiri.project.utils.http.AppUrls
import com.jasiri.project.utils.http.AppUrls.addCommonParams
import com.jasiri.project.utils.http.HttpHelper
import com.jasiri.project.utils.http.IHttpCallback
import org.json.JSONObject


/**
 * TODO: Analysis Lib 初始化时机
 */
object TestAnalysisInit {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalysisBuilder.pathGoogleInstall = "/s9Fzx3hTHSaxTL3lHmXEC6g"
        AnalysisBuilder.pathAdjustInstall = "/PfUV4QgcwXcDLVTEwks91A"
        AnalysisBuilder.pathFacebookInstall = "/nY1OQ5JbghFbhnNdH15XzA"
        AnalysisBuilder.pathFirebaseInstall = "/s81AdW2geElxhlAS3cTOw"

        AnalyticsUtil.init(BaseApp.application)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
//        AnalysisBuilder.host = AppUrls.APP_URL
//        AnalysisBuilder.zone = Configs.getSPString(Configs.SPKEY.JASIRI_TIMEZONE)
        AnalyticsUtil.init(BaseApp.application)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalysisBuilder.token = Configs.getSPString(Configs.SPKEY.JASIRI_TOKEN)
        AnalysisBuilder.userId = Configs.getSPString(Configs.SPKEY.JASIRI_USERID)
        AnalyticsUtil.init(BaseApp.application)
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {
    /**
     * TODO: 在首页每次点击申请时（在已登录的前提下），调用一下 data lib 的上报
     * 获取设备数据上传状态，此方法只会在首页点申请时调用
     */
    fun loadDInfoTypeAction(){
        val params = JSONObject().addCommonParams().apply {
            put("path", AppUrls.PATH.DEVICE_INFO_TYPES)
        }
        val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
        HttpHelper.post(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            val deviceInfoTypes = obj.optJSONArray("deviceInfoTypes")
                            val typeList = arrayListOf<String>()
                            deviceInfoTypes?.let {
                                //需要手动添加 位置信息和陀螺仪 数据上报
                                it.put("T11")
                                it.put("T12")
                                for (i in 0..< it.length()){
                                    typeList.add(it.optString(i))
                                }
                            }
                            //上报设备数据
                            JasiriSysDataHelper.onCheckSubmitDataByTypes(App.instance(),typeList,"Limit Page")
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}